<?php


namespace App\Service;


class HtmlExcel
{

    // 影票导出
    public function profit_explode()
    {
        // 整理
        $result = [];
        foreach($lists as $item){
            $result[$item['uid']] = [
                'uid'      => $item['uid'],
                'nickname' => $item['userinfo']['user_nickname'],
            ];
        }

        //表头
        $headList = [
            'uid'      => '会员ID',
            'nickname' => '用户昵称',
        ];

        array_unshift($result, $headList);
        $xlxTitle  = "映票汇总";
        $headTitle = $xlxTitle;
        $headtitle = "<tr style='height:50px;border-style:none;'><th border=\"0\" style='height:60px;font-size:26px;' colspan='".count($headList)."' >{$headTitle}</th></tr>";
        $headtitle = "<tr style='height:40px;'><th border=\"0\" style='height:40px;font-size:26px;' colspan='".count($headList)."' >{$headTitle}</th></tr>";
        $filename  = $xlxTitle.".xls";

        $str = $this->excelData($result, $headtitle, $filename);
        exit($str);
    }


    // 表头
    public function tableHeader($headTitle, $headList)
    {
        return "<tr style='height:40px;'><th border=\"0\" style='height:40px;font-size:26px;' colspan='".count($headList)."' >{$headTitle}</th></tr>";
    }

    /**
     *处理Excel导出
     * @param $datas array 设置表格数据
     * @param $titlename string 设置head
     * @param $title string 设置表头
     */
    public function excelData($datas, $title, $filename)
    {
        $str = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\nxmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\nxmlns=\"http://www.w3.org/TR/REC-html40\">\r\n<head>\r\n<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body>";
        $str .= "<style>tr,td,th{text-align: center;height: 26px;line-height: 26px;}td{min-width: 250px;}</style>";
        $str .= "<table border=1>";
        $str .= $title;
        foreach($datas as $key => $rt){
            $str .= "<tr>";
            foreach($rt as $k => $v){
                $str .= "<td>{$v}</td>";
            }
            $str .= "</tr>\n";
        }
        $str .= "</table></body></html>";
        header("Content-Type: application/vnd.ms-excel; name='excel'");
        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=".$filename);
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        header("Expires: 0");

        return $str;
    }
}


